package test.更新pm4年份相关;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.ds.druid.DruidDSFactory;
import lombok.SneakyThrows;
import utils.SmallTool;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: gcr
 * @Date: 2022-6-3 14:28
 */
public class UpdateHtmlYear {
    @SneakyThrows
    public static void main(String[] args) {

        DSFactory.setCurrentDSFactory(new DruidDSFactory());
        DataSource ds = DSFactory.get("server");
        Db useServer = Db.use(ds);

        String localPm = "pubmed";
        String server = "pubmed_analysis";
        String sql = "select max(pmid) as count from " + localPm;
        Integer max = Integer.parseInt(useServer.queryString(sql));
        StackTraceElement[] stackTrace = new Throwable().getStackTrace();
        String fileName = stackTrace[0].getFileName();
        SmallTool.printMess("开始了+++" + fileName.replace(".java", ""));


        int breakSize = 10000;
        int page = max / breakSize + 1;
        SmallTool.printMess("最大值+++" + max);
        SmallTool.printMess("页码+++" + page);
        for (int i = 1; i <= page; i++) {
            int start = breakSize * (i - 1) + 1;
            int end = breakSize * i;
            SmallTool.printMess("开始页码+" + i + "+++" + start + "++++end+++" + end);
            String querySql = "select pmid,html_year from " + localPm + " where pmid between " + start + " and " + end;
            List<Entity> queryMain = useServer.query(querySql);
            Map<Integer, String> localPmFi = new ConcurrentHashMap<>();
            for (Entity entity : queryMain) {
                String html_year = entity.getStr("html_year");
                if (html_year != null) {

                    localPmFi.put(entity.getInt("pmid"), entity.getStr("html_year"));
                }
            }
            localPmFi.forEach((k, v) -> {
                String updateSql = "update " + server + " set html_year = " + v + " where pmid = " + k;
                try {
                    useServer.execute(updateSql);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            });
        }

    }
}
